<?php
require_once("DBConnection.php");

class RegistrationHandler{
	
	private $db = NULL; 
	
	const BDATE_WRONG_FORMAT = 0; //onstanetr för felmeddelanden
	const USERNAME_TAKEN = 1;
	const USERNAME_LENGTH = 2;
	const PASS_LENGTH = 3;
	const PASS_NOT_MATCH = 4;
	const USERNAME_CHARACTERS = 5;
	
	public function __construct(DBConnection $db){
		$this->db = $db;
	}

	public function CheckBDate($date){

		$regex1 = "/^([12]{1}[0-9]{3}){1}-(([0][1-9])|([1][0-2])){1}-(([0][1-9])|([12][0-9])|([3][0-2])){1}$/i"; //regex för formatet 1990-04-21
		$regex2 = "/^([0-9]{2}){1}-(([0][1-9])|([1][0-2])){1}-(([0][1-9])|([12][0-9])|([3][0-2])){1}$/i"; //regex för formatet 90-04-21
		$regex3 = "/^([0-9]{2}){1}(([0][1-9])|([1][0-2])){1}(([0][1-9])|([12][0-9])|([3][0-2])){1}$/i"; //regex för formatet 900421

		$month;
		$day;

		if(preg_match($regex1, $date )){ //matchar datumet i format nummer 1 så.......
			$month = substr($date, 5,2); //hämtar vi månaden på positionen 5-7.
			$day = substr($date, 8,2); //hämtar dag på position 8-10
		}else if(preg_match($regex2, $date )){ //samma för format 2
			$month = substr($date, 3,2);
			$day = substr($date, 6,2);
		}else if(preg_match($regex3, $date )){ //samma för format 3
			$month = substr($date, 2,2);
			$day = substr($date, 4,2);
		}else{ //om datumet inte stämmer överens med något av formaten returnerar funktionen false
			return false;
		}

		switch($month){
			case '01' || '03' || '05' || '07' || '08' || '10' || '12': //om
				if ($day > 31) return false; break;
			case '02':
				if ($day > 28) return false; break;
			default:
				if ($day > 30) return false; break;
		}

		return true;
	}
	
	public function checkUsername($username){
		$sql = "SELECT * FROM user WHERE username = ?";
		
		$stmt = $this->db->Prepare($sql);
		
		$stmt->bind_param('s', $username);
		
		if($this->db->execute($stmt) > 0){
			return false;
		}
		return true;
		
	}
	
	public function checkPasswords($p1, $p2){
		if($p1 == $p2){
			return true;
		}else{
			return false;
		}
	}
	public function checkPasswordLength($pass){
		$regex = "/^.{3,12}$/"; //kollar längden på pword-strängen
		if(preg_match($regex, $pass)){
			return true;
		}else{
			return false;
		}
	}
	public function checkUsernameLength($user){
		$regex = "/^.{3,12}$/"; //kollar längden på username
		if(preg_match($regex, $user)){
			return true;
		}else{
			return false;
		}
	}
	public function checkUsernameChars($user){
		$regex = "/^[a-zA-Z0-9_-]*$/"; //kollar så anvnamn inte innehåller felaktiga tecken
		if(preg_match($regex, $user)){
			return true;
		}else{
			return false;
		}
	}
	
	public function addUser($realname, $bdate, $username, $password){
		$sql  = "INSERT INTO user(username, password, bdate, realname) VALUES(?, ?, ?, ?)";
		
		$stmt = $this->db->Prepare($sql);
		
		$stmt->bind_param('ssss', $username, $password, $bdate, $realname);
		
		if($this->db->execute($stmt) === 0){
			return true;
		}
		return false;
	}
	
	public function removeUser($username){
		
		$sqlQuery = "DELETE FROM user WHERE username = ?";
			
		$stmt = $this->db->Prepare($sqlQuery);
			
		$stmt->bind_param('s', $username);
			
		$this->db->execute($stmt);
	}

}		
?>